﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Adex;
using System.IO;
using MediaMonitor.Common;


namespace MediaMonitor.Admin
{


    public partial class Data_Mgmt_Product : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = Adex.VProduct.Query().SetSelectList("*, CONVERT(VARCHAR, DateModified, 101) AS LastModifiedDate ").ExecuteDataSet().Tables[0];
            radGrid.DataSource = dt;
        }

        protected void radGrid_DeleteCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

            //check deletion of child rows
            if (e.Item.OwnerTableView.DataSourceID == "sqlDS")
            {
                string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProdVersionCode"].ToString();
                try
                {
                    ProdVersion.Delete(detailPkId);
                }
                catch
                {
                    AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
                }

                return;
            }



            string pkId = radGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["ProductCode"].ToString();

            try
            {
                Product.Delete(pkId);

            }
            catch
            {
                AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
            }

            radGrid.Rebind();
        }

        protected void DelShortVideo(object sender, EventArgs e)
        {
            string pkID = ((LinkButton)(sender)).CommandArgument;

            if (pkID.Length > 0)
            {

                VProdVersion vpv = new VProdVersion("ProdVersionID", pkID);
                ProdVersion pv = new ProdVersion(pkID);
                try
                {

                    //del physical files
                    if (File.Exists(Server.MapPath("../videos/" + vpv.ProductCode) + "/" + vpv.ShortVideoFile))
                        File.Delete(Server.MapPath("../videos/" + vpv.ProductCode) + "/" + vpv.ShortVideoFile);

                    pv.ShortVideo = false;
                    pv.ShortVideoFile = "";

                    pv.Save();
                    radGrid.Rebind();

                    radPanel.Alert("selected file deleted successfully");
                }

                catch (Exception ex)
                {
                    radPanel.Alert("Could not delete the selected file. [" + ex.Message + "]");

                }

            }
        }


        protected void DelFullVideo(object sender, EventArgs e)
        {
            string pkID = ((LinkButton)(sender)).CommandArgument;

            if (pkID.Length > 0)
            {

                VProdVersion vpv = new VProdVersion("ProdVersionID", pkID);
                ProdVersion pv = new ProdVersion(pkID);

                try
                {

                    //del physical files
                    if (File.Exists(Server.MapPath("../videos/" + vpv.ProductCode) + "/" + vpv.FullVideoFile))
                        File.Delete(Server.MapPath("../videos/" + vpv.ProductCode) + "/" + vpv.FullVideoFile);

                    pv.FullVideo = false;
                    pv.FullVideoFile = "";

                    pv.Save();
                    radGrid.Rebind();

                    radPanel.Alert("selected file deleted successfully");

                }

                catch (Exception ex)
                {
                    radPanel.Alert("Could not delete the selected file. [" + ex.Message + "]");


                }

            }
        }

    }
}